<template>
  <!-- 加入网络 -->
  <el-dialog :title="title" :visible.sync="open" width="460px" append-to-body :close-on-click-modal="false">
    <div class="disFlxAC">
      <div class="shrink0">网络邀请码：</div>
      <el-input v-model="getParams.networkCode" placeholder="请输入网络管理员提供的6位数邀请码"  clearable>
        <el-button slot="append" icon="el-icon-search" @click="searchMesh"></el-button>
      </el-input>
    </div>
    <el-card v-if="networkInfo" class="mt-15">
      <div class="disFlxAC">
        <img :src="networkInfo.networkIcon" style="width:50px;height:50px;" />
        <div class="ml-15">{{ networkInfo.networkName }}</div>
      </div>
    </el-card>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确定加入</el-button>
      <el-button @click="open = false">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getNetworkList, addNetworkMember } from '@/api/mesh/index.js'
export default {
  data () {
    return {
      title: "加入网络",
      open: false,
      getParams: {
        pageNum: 1,
        pageSize: 999,
        networkCode: ""
      },
      networkInfo: undefined,
    }
  },
  methods: {
    openDialog() {
      this.getParams.networkCode = "";
      this.open = true;
    },
    searchMesh(){
      getNetworkList(this.getParams).then(res=>{
        if(res.rows && res.rows.length){
          this.networkInfo = res.rows[0]
        }else{
          this.networkInfo = undefined;
          this.$message.error('未查询到该网络信息');
        }
      })
    },
    submitForm() {
      if(!this.networkInfo){
        this.$message.error('请输入网络邀请码并查询');
        return
      }
      const { comId } = this.$store.state.user.userInfo;
      addNetworkMember({
        networkId: this.networkInfo.networkId,
        companyIds: [comId],
        memberType:  2, // 成员类型: 0创建人 1负责单位 2成员
      }).then((res) => {
        this.$message.success("加入网络成功");
        this.open = false;
        this.$emit('refresh')
      });
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
